﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Tesseract;
using APP.IO;
using System.Threading;

namespace MinesweeperAutoScanDemo
{


    public class PictureItems
    {

    }

    public class PersonEnumerator : IEnumerable<PictureItems>
    {
        public List<PictureItems> people { get; set; }

        // 实现 IEnumerable<T>的 IEnumerator<T> GetEnumerator()方法
        IEnumerator<PictureItems> IEnumerable<PictureItems>.GetEnumerator()
        {
            return ((IEnumerable<PictureItems>)people).GetEnumerator();
        }

        // 由于IEnumerable<out T> : IEnumerable，所以还需要实现 IEnumerable的 IEnumerator GetEnumerator();
        IEnumerator IEnumerable.GetEnumerator()
        {
            return people.GetEnumerator();
        }
    }

    public partial class ImageChip
    {
        public int Row { get; set; }
        public int Column { get; set; }

        public Bitmap Image { get; set; }

        public string DeBUGPath { get; set; }

        public string OCRText { get; set; }

        public Rectangle Rect { get; set; }
    }

    public class OCRHelper
    {


        public static List<ImageChip> SplitImage(Image fromImage, int Columns, int rows)
        {
            var result = new List<ImageChip>();
            var chipWidth = fromImage.Width * 1f / Columns;
            var chipHeight = fromImage.Height * 1f / rows;

            var folder = PathHelper.GetPath(FolderType.Temp, "ImageTest", DateTime.Now.ToString("yyyyMMddHHmmssffffff"));
            for (int row = 0; row < rows; row++)
            {
                for (int column = 0; column < Columns; column++)
                {
                    try
                    {
                        var chip = new ImageChip();
                        var itemX = column * chipWidth;
                        var itemY = row * chipHeight;

                        chip.Row = row;
                        chip.Column = column;


                        var chipRect = new Rectangle((int)itemX, (int)itemY, (int)chipWidth, (int)chipHeight);
                        chip.Rect = chipRect;
                        //创建新图位图   
                        Bitmap bitmap = new Bitmap((int)chipWidth, (int)chipHeight);
                        //创建作图区域   
                        Graphics graphic = Graphics.FromImage(bitmap);
                        //截取原图相应区域写入作图区   
                        graphic.DrawImage(fromImage, 0, 0, chipRect, GraphicsUnit.Pixel);
                        //从作图区生成新图   
                        Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
                        var saveFilePath = Path.Combine(folder, $"{row}_{column}.jpg");
                        //保存图象   
                        saveImage.Save(saveFilePath, System.Drawing.Imaging.ImageFormat.Jpeg);

                        chip.Image = bitmap;
                        chip.DeBUGPath = saveFilePath;
                        //释放资源   
                        saveImage.Dispose();
                        graphic.Dispose();

                        result.Add(chip);
                    }
                    catch (Exception ex)
                    {
                        Thread.Sleep(50);
                        Console.WriteLine(ex);
                    }
                }

            }
            return result;
        }

        public static List<ImageChip> SplitImageTestDrawLine(Image fromImage, int Columns, int rows)
        {
            var result = new List<ImageChip>();
            var chipWidth = fromImage.Width * 1f / Columns;
            var chipHeight = fromImage.Height * 1f / rows;

            var folder = PathHelper.GetPath(FolderType.Temp, "ImageTest");

            Graphics graphic = Graphics.FromImage(fromImage);
            for (int row = 0; row < rows; row++)
            {
                var itemY = row * chipHeight;
                graphic.DrawLine(new Pen(Brushes.Red), 0, itemY, fromImage.Width * 1, itemY);
            }
            for (int column = 0; column < Columns; column++)
            {
                var itemX = column * chipWidth;
                graphic.DrawLine(new Pen(Brushes.Red), itemX, 0, itemX, fromImage.Height);
            }
            var tempPath = PathHelper.GetTempFileName(folder, "text.png");
            fromImage.Save(tempPath);
            graphic.Dispose();

            return result;
        }



    }
}
